<?php

namespace App\Models;


class OrderGoods extends BaseModel
{
    protected $table = "order_goods";

    protected $rules = [
        'admin_id' => 'required',
        'shop_id' => 'required',
        'goods_id' => 'required',
        'goods_name' => 'required|between:1,20',
        'goods_price' => ['required', 'numeric', 'regex:/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])/'],
        'shop_price' => 'numeric',
        'goods_num' => 'required|numeric|min:1',
        'category_id' => 'required|numeric',
        'brand_id' => 'required|numeric',
        'bucket_category_id' => 'required|numeric',
    ];

    protected $message = [
        'admin_id.required' => '用户ID获取失败',
        'shop_id.required' => '店铺ID获取失败',
        'goods_id.required' => '商品ID获取失败',
        'goods_name.required' => '商品名称必须填写',
        'goods_name.between' => '商品名称不能超过20个字',
        'goods_price.required' => '商品价格必须填写',
        'goods_price.numeric' => '商品价格必须是纯数字',
        'goods_price.regex' => '商品价格不能小于0',
        'shop_price.numeric' => '市场价格必须是纯数字',
        'goods_num.required' => '商品库存必须填写',
        'goods_num.numeric' => '商品库存必须是纯数字',
        'goods_num.min' => '商品库存不能小于1',
        'category_id.required' => '请选择商品所属分类',
        'category_id.numeric' => '非法分类数据',
        'brand_id.required' => '请选择商品所属品牌',
        'brand_id.numeric' => '非法品牌数据',
        'bucket_category_id.required' => '请选择商品所属水桶分类',
        'bucket_category_id.numeric' => '非法水桶分类数据',
    ];

    public function waterBill()
    {
        return $this->hasOne('App\Models\WaterBill', 'goods_id', 'goods_id');
    }


    public function getOrderList()
    {
        $keyword = request('keyword');
        if ($keyword) {
            $where['order_sn'] = ['like', '%' . $keyword . '%'];
        }
        $rows = $this->_getPageList($where);
        $txtStr['status'] = [0 => '已取消', 10 => '未付款', 20 => '已付款'];
        $rows['data'] = int_to_string($rows['data'], $txtStr);
        return $rows;
    }

    public function getOwnOrderList()
    {
        $keyword = request('keyword');
        $where['admin_id'] = is_login();
        if ($keyword) {
                $where['order_sn'] = ['like', '%' . $keyword . '%'];
        }
        $rows = $this->_getPageList($where);
        $txtStr['status'] = [1 => '上架', -1 => '下架', 0 => '待审核'];
        $rows['data'] = int_to_string($rows['data'], $txtStr);
        return $rows;
    }

    public function getOwnOrderListWithBill()
    {
        $keyword = request('keyword');
        $where['admin_id'] = is_login();
        $where['shop_id'] = request('shop_id');
        if ($keyword) {
            $where['order_sn'] = ['like', '%' . $keyword . '%'];
        }
        return $this->_getJoinPageList('waterBill',$where,'*',['sort'=>'asc'],10,5,'id,goods_id,waterbill_category,status',['shop_id'=>$where['shop_id']]);
    }

    public function getOrderListByShopId($shopId)
    {
        $keyword = request('keyword');
        if ($keyword) {
            $where['order_sn'] = ['like', '%' . $keyword . '%'];
        }
        if (request('category_id')) {
            $where['category_id'] = request('category_id');
        }
        $where['shop_id'] = $shopId;
        $where['status'] = ['>', 0];
        $rows = $this->_getPageList($where, 'id,goods_id,goods_name,goods_num,goods_price');
        return $rows;
    }



    public function addOrUpdate()
    {
        $data['admin_id'] = is_login();
        $data['shop_id'] = intval(trim(request('shop_id')));
        $data['goods_id'] = intval(trim(request('goods_id')));
        $data['goods_name'] = trim(request('goods_name'));
        $data['goods_price'] = floatval(trim(request('goods_price', 1)));
        $data['shop_price'] = floatval(trim(request('shop_price')));
        $data['goods_num'] = intval(trim(request('goods_num', 1)));
        $data['pic_num'] = count(request('pics'));
        $data['category_id'] = request('category_id');
        $data['bucket_category_id'] = request('bucket_category_id');
        $data['brand_id'] = request('brand_id');
        $data['goods_content'] = request('goods_content', '');
        $data['status'] = request('status', 0);
        if (request('id')) {
            $data['id'] = trim(request('id'));
            $this->rules['goods_name'] .= ',' . $data['id'] . ',id,' . 'shop_id,' . $data['shop_id']; //唯一字段验证,在更新时排除当前店铺中的当前商品id
        } else {
            $data['addtime'] = time();
        }
        if (!$this->_validate($data)) {
            return false;
        }
        //开启事务
        $this->_start();
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '保存商品信息失败！';
            $this->_rollBack(); //回滚
            return false;
        }
        if (!isset($data['id'])) {
            //新增商品同时更新shop表商品总数
            $shop = new Shop();
            $shop->_inc(['id' => $data['shop_id']], 'total_goods_num');
        }
        $id = isset($data['id']) ? $data['id'] : $this->id;
        $picture = new Picture();
        $res = $picture->upload($id);
        if (!$res) {
            $this->error = $picture->getError();
            $this->_rollBack(); //回滚
            return false;
        }
        //记录行为
        action_log('update_goods', 'goods', $id);
        //提交事务
        $this->_commit();
        return $res;
    }


}
